package com.lib.framework.module.glide

import android.content.Context
import android.graphics.drawable.Drawable
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.RequestBuilder
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners

import com.bumptech.glide.request.RequestOptions
import com.lib.framework.manager.thread.bgThreadRun
import com.lib.framework.manager.thread.mainThreadRun


/**
 * Copyright (C), 2019-2020
 * FileName: GlideHlper
 * Author: Kevin
 * Date: 2020-01-09 18:13
 * Description: Glide 辅助类
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */
class GlideHlper {

    companion object {

        /**
         * 加载图片
         *
         * @param imgRedId 图片资源Id
         * @param image ImageView
         */
        fun load(context: Context, imgRedId: Int, image: ImageView) {
            Glide.with(context).load(imgRedId).into(image)
        }

        /**
         * 加载图片
         *
         * @param imgRedId 图片资源Id
         * @param image ImageView
         */
        fun loadCircle(context: Context, imgRedId: Int, image: ImageView) {
            Glide.with(context).load(imgRedId).apply(RequestOptions.bitmapTransform(CircleCrop())).into(image)
        }

        /**
         * 加载图片
         *
         * @param imgRedId 图片资源Id
         * @param image ImageView
         * @param roundingRadius 圆角角度
         */
        fun loadRound(context: Context, imgRedId: Int, image: ImageView, roundingRadius: Int) {
            Glide.with(context).load(imgRedId).apply(RequestOptions.bitmapTransform(RoundedCorners(roundingRadius)))
                .into(image)
        }

        /**
         * 加载图片
         *
         * @param url 图片地址
         * @param image ImageView
         * @param placeholderResId 占位符图片资源id
         * @param errorResId 错误图片资源id
         * @param fallbackResId fallback 图片资源id
         * @param thumbnailUrl 缩略图url，缩略图和原图请求并行启动，如果原图请求在缩略图请求之前完成，则缩略图请求中的图像将不会被展示。
         */
        fun load(context: Context, url: String, image: ImageView, placeholderResId: Int? = null,
                 errorResId: Int? = null, fallbackResId: Int? = null,
                 thumbnailUrl: String? = null) {
            var requestBuilder = Glide.with(context).load(url)
            requestBuilder = setPlaceholder(context, requestBuilder, placeholderResId, errorResId,
                fallbackResId, thumbnailUrl)
            requestBuilder.into(image)
        }

        /**
         * 加载圆形图片
         *
         * @param url 图片地址
         * @param image ImageView
         * @param placeholderResId 占位符图片资源id
         * @param errorResId 错误图片资源id
         * @param fallbackResId fallback 图片资源id
         * @param thumbnailUrl 缩略图url，缩略图和原图请求并行启动，如果原图请求在缩略图请求之前完成，则缩略图请求中的图像将不会被展示。
         */
        fun loadCircle(context: Context, url: String, image: ImageView,
                       placeholderResId: Int? = null, errorResId: Int? = null,
                       fallbackResId: Int? = null, thumbnailUrl: String? = null) {
            var requestBuilder = Glide.with(context).load(url)

            requestBuilder = setPlaceholder(context, requestBuilder, placeholderResId, errorResId,
                fallbackResId, thumbnailUrl)

            requestBuilder.apply(RequestOptions.bitmapTransform(CircleCrop())).into(image)

            requestBuilder.into(image)
        }

        /**
         * 加载圆角图片
         *
         * @param url 图片地址
         * @param image ImageView
         * @param roundingRadius 圆角角度
         * @param placeholderResId 占位符图片资源id
         * @param errorResId 错误图片资源id
         * @param fallbackResId fallback 图片资源id
         * @param thumbnailUrl 缩略图url，缩略图和原图请求并行启动，如果原图请求在缩略图请求之前完成，则缩略图请求中的图像将不会被展示。
         */
        fun loadRound(context: Context, url: String, image: ImageView, roundingRadius: Int,
                      placeholderResId: Int? = null, errorResId: Int? = null,
                      fallbackResId: Int? = null, thumbnailUrl: String? = null) {
            var requestBuilder = Glide.with(context).load(url)

            requestBuilder = setPlaceholder(context, requestBuilder, placeholderResId, errorResId,
                fallbackResId, thumbnailUrl)

            requestBuilder.apply(RequestOptions.bitmapTransform(RoundedCorners(roundingRadius)))
                .into(image)

            requestBuilder.into(image)
        }

        /**
         * 公共设置，设置占位符、错误、后备回调符、缩略图 图片
         */
        private fun setPlaceholder(context: Context, requestBuilder: RequestBuilder<Drawable>,
                                   placeholderResId: Int? = null, errorResId: Int? = null,
                                   fallbackResId: Int? = null,
                                   thumbnailUrl: String? = null): RequestBuilder<Drawable> {
            var requestBuilderTmp = requestBuilder
            if (placeholderResId != null) {
                requestBuilderTmp = requestBuilderTmp.placeholder(placeholderResId)
            }
            if (errorResId != null) {
                requestBuilderTmp = requestBuilderTmp.error(errorResId)
            }
            if (fallbackResId != null) {
                requestBuilderTmp = requestBuilderTmp.fallback(fallbackResId)
            }

            if (thumbnailUrl != null) {
                requestBuilderTmp = requestBuilderTmp.thumbnail(
                    Glide.with(context).load(thumbnailUrl))
            }

            return requestBuilderTmp
        }

        /**
         * 清除缓存
         */
        fun clearCache(context: Context) {
            mainThreadRun {
                // 必须在主线程调用
                Glide.get(context.applicationContext).clearMemory()
            }
            bgThreadRun {
                Glide.get(context.applicationContext).clearDiskCache()
            }
        }
    }
}